<!-- <template>
  <div class="big-box">
    <el-card shadow="never" class="table-container">
      <el-table
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
      >
        <el-table-column
          label="提交人"
          prop="name"
          width="120"
          align="center"
        />
        <el-table-column label="建议标题" align="center" prop="title" />
        <el-table-column
          label="手机号码"
          prop="phone"
          align="center"
          width="150"
        />
        <el-table-column
          label="提交时间"
          prop="time"
          align="center"
          width="200"
        />
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template #default="">
            <el-button type="primary" link size="small"
              ><i-ep-view />查看</el-button
            >
            <el-button type="primary" link size="small"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination />
    </el-card>
  </div>
</template> -->

<script setup lang="ts">
import { getFreebackPage, deleteFreeback } from "@/api/suggestion/index";
import { FreebackQuery, FreebackPageVO } from "@/api/suggestion/types";

const queryFormRef = ref(ElForm); // 查询表单

const viewDialog = ref(false); //输入框禁用
const loading = ref(false); //加载状态
const queryParams = reactive<FreebackQuery>({
  page: 1,
  pageSize: 10,
});
const startTime = ref("");
const endTime = ref("");
const total = ref(0); // 数据总数
const pageData = ref<FreebackPageVO[]>(); // 意见反馈分页数据

watch(startTime, (newVal) => {
  if (newVal) {
    queryParams.startTime = newVal;
  }
});
watch(endTime, (newVal) => {
  if (newVal) {
    queryParams.endTime = newVal;
  }
});

// 弹窗对象
const dialog = reactive({
  visible: false,
  width: 800,
  title: "",
});

// 意见反馈表单数据
const formData = reactive<FreebackPageVO>({});

/** 查询 */
function handleQuery() {
  loading.value = true;
  getFreebackPage(queryParams)
    .then(({ data }) => {
      pageData.value = data.records;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */
function resetQuery() {
  queryFormRef.value.resetFields();
  startTime.value = "";
  endTime.value = "";
  queryParams.page = 1;
  queryParams.startTime = undefined;
  queryParams.endTime = undefined;
  handleQuery();
}

/**打开弹窗 */
function openDialog(row: any) {
  dialog.visible = true;
  Object.assign(formData, { ...row });
  viewDialog.value = true;
  dialog.title = "意见反馈";
}

/**
 * 关闭弹窗
 *
 * @param
 */
function closeDialog() {
  dialog.visible = false;
  formData.id = undefined;
  viewDialog.value = false;
}

/** 删除用户 */
function handleDelete(id?: string) {
  if (id) {
    ElMessageBox.confirm("确认删除管理员?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(function () {
      deleteFreeback(id).then(() => {
        ElMessage.success("删除成功");
        resetQuery();
      });
    });
  }
}
onMounted(() => {
  handleQuery();
});
</script>

<template>
  <div class="app-container">
    <div class="search-container">
      <el-form
        ref="queryFormRef"
        :model="queryParams"
        :inline="true"
        label-width="100px"
        class="form-container"
      >
        <el-form-item label="提交人" prop="name">
          <el-input
            placeholder="提交人"
            clearable
            style="width: 200px"
            v-model="queryParams.name"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="提交时间">
          <el-date-picker
            v-model="startTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="开始时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
          <span class="time">~</span>
          <el-date-picker
            v-model="endTime"
            class="!w-[200px]"
            type="datetime"
            placeholder="截止时间"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="建议标题" prop="title">
          <el-input
            placeholder="建议标题"
            clearable
            style="width: 200px"
            v-model="queryParams.title"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input
            placeholder="手机号码"
            clearable
            style="width: 200px"
            v-model="queryParams.phone"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item class="handleQuery">
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <el-table
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
      >
        <el-table-column
          label="提交人"
          prop="name"
          width="120"
          align="center"
        />
        <!-- show-overflow-tooltip -->
        <el-table-column label="建议标题" align="center" prop="title" />
        <el-table-column
          label="手机号码"
          prop="phone"
          align="center"
          width="150"
        />
        <el-table-column
          label="提交时间"
          prop="time"
          align="center"
          width="200"
        />
        <el-table-column fixed="right" label="操作" align="center" width="200">
          <template #default="scope">
            <el-button
              type="primary"
              link
              size="small"
              @click="openDialog(scope.row)"
              ><i-ep-view />查看</el-button
            >
            <el-button
              type="primary"
              link
              size="small"
              @click="openDialog(scope.row)"
              ><i-ep-view />修改</el-button
            >
            <!-- v-hasPerm="['sys:dict_type:delete']" -->
            <el-button
              type="primary"
              link
              size="small"
              @click="handleDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.page"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
    <!-- 弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :width="dialog.width"
      append-to-body
      @close="closeDialog"
      :title="dialog.title"
    >
      <!-- 查看 -->
      <el-form :model="formData" label-width="100px" ref="formRef">
        <el-form-item label="建议标题" prop="title">
          <el-input v-model="formData.title" :disabled="viewDialog" />
        </el-form-item>
        <el-form-item label="建议内容" prop="content">
          <el-input
            :rows="3"
            type="textarea"
            v-model="formData.content"
            :disabled="viewDialog"
          />
        </el-form-item>
        <el-form-item label="提交人" prop="name">
          <el-input v-model="formData.name" :disabled="viewDialog" />
        </el-form-item>
        <el-form-item label="提交时间" prop="time">
          <el-date-picker
            type="date"
            style="width: 100%"
            v-model="formData.time"
            :disabled="viewDialog"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="formData.phone" :disabled="viewDialog" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="closeDialog">确 认</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.dialog-footer button:first-child {
  margin-right: 10px;
}

.form-container {
  display: flex;
  flex-wrap: wrap;
}

.handleQuery {
  margin-left: auto;
}

.time {
  margin: 0 10px;
}
</style>
